package com.meida.student.app.user.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.SaveInterceptor;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.ApiAssert;
import com.meida.module.bus.client.entity.BusOrder;
import com.meida.module.bus.client.entity.BusOrderAnswer;
import com.meida.module.bus.client.entity.BusOrderAppraise;
import com.meida.module.bus.client.enums.OrderSearchStateEnum;
import com.meida.module.bus.client.enums.OrderStatePEnum;
import com.meida.module.bus.provider.service.BusOrderAppraiseService;
import com.meida.module.bus.provider.service.BusOrderService;
import com.meida.module.bus.provider.utils.SensitivewordFilter;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 订单评价
 *
 * @author zyf
 */
@Component
public class OrderAppraiseSaveHandler implements SaveInterceptor<BusOrderAppraise> {
    @Autowired
    private BusOrderService busOrderService;
    @Autowired
    private BusOrderAppraiseService busOrderAppraiseService;
    @Autowired
    private SensitivewordFilter sensitivewordFilter;

    @Override
    public ResultBody validate(CriteriaSave cs, EntityMap params) {
        BusOrderAppraise busOrderAppraise = cs.getEntity(BusOrderAppraise.class);
        Long orderId = busOrderAppraise.getOrderId();
        ApiAssert.isNotEmpty("缺少订单参数", orderId);
        BusOrder busOrder = busOrderService.getById(orderId);
        Long answerUserId = busOrder.getAnswerUserId();
        if (FlymeUtils.isEmpty(busOrder)) {
            return ResultBody.failed("订单不存在");
        }
        String orderState = busOrder.getOrderState();
        if (!orderState.equals("5")) {
            return ResultBody.failed("当前订单不可评价");
        }
        ApiAssert.isNotEmpty("该订单不存在", busOrder);
        Long appraiseNum = busOrderAppraiseService.countNumByOrderId(orderId);
        ApiAssert.isFalse("该订单已评价", appraiseNum > 0);
        BigDecimal score = busOrderAppraise.getScore();
        ApiAssert.isNotEmpty("该订单已评价", score);
        cs.put("answerUserId", answerUserId);
        cs.put("appraiseNum", appraiseNum);
        String appraiseContent = busOrderAppraise.getAppraiseContent();
        if (FlymeUtils.isNotEmpty(appraiseContent)) {
            Boolean isContainsSensitive = sensitivewordFilter.isContainsSensitive(appraiseContent);
            if (isContainsSensitive) {
                return ResultBody.failed("评价内容包含敏感词,请重新编辑评价");
            }
        }
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaSave criteriaSave, EntityMap entityMap, BusOrderAppraise busOrderAppraise) {
        Long answerUserId = criteriaSave.getLong("answerUserId");
        busOrderAppraise.setAnswerUserId(answerUserId);
    }

}
